package demo.aop;

import javax.annotation.PostConstruct;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author wyb
 */
@Component
@Aspect
public class LogAspect {

    @Pointcut("execution(@demo.aop.Log public * demo.service.*.*(..))")
    private void pointcut() {
    }

    @Around("pointcut() && @annotation(log)")
    public Object around(ProceedingJoinPoint joinPoint, Log log) throws Throwable {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();

        long start = System.currentTimeMillis();
        Object retVal = joinPoint.proceed();

        long duration = System.currentTimeMillis() - start;
        System.out.println(String.format(
                "%s::%s elapse time: %d ms, msg: %s", className, methodName, duration, log.msg()));

        return retVal;
    }

}
